//-----------------------------------------------------------------------------
// Window_ShopStatus
//
// The window for displaying number of items in possession and the actor's
// equipment on the shop screen.

Window_ShopStatus.prototype.initialize = function(x, y, width, height) {
    Window_Base.prototype.initialize.call(this, x, y, width, height);
    this._item = null;
    this.refresh();
    this._actorSprite = [];
    this.createHeadSprites();
};

Window_ShopStatus.prototype.createHeadSprites = function() {
    for(var actorId = 0; actorId < $gameParty.members().length; ++actorId){
        this._actorSprite[actorId] = new Sprite();
        this.addChild(this._actorSprite[actorId]);
    }
};

Window_ShopStatus.prototype.clearHeadSprites = function() {
    if (!this._actorSprite) return;
    for(var i = 0; i < this._actorSprite.length; ++i){
        this._actorSprite[i].opacity = 0;
    }
};

Window_ShopStatus.prototype.refresh = function() {
    this.contents.clear();
    this.clearHeadSprites();
    if (!this._item) return;
    this.resetTextColor();
    this.resetFontSettings();
    if (!this.isEquipItem()) return;
    this.resetTextColor();
    this.resetFontSettings();
    this.drawActorData();
};

Window_ShopStatus.prototype.setItem = function(item) {
    this._item = item;
    this.refresh();
};

Window_ShopStatus.prototype.isEquipItem = function() {
    return DataManager.isWeapon(this._item) || DataManager.isArmor(this._item);
};

Window_ShopStatus.prototype.currentEquippedItem = function(actor, etypeId) {
    var list = [];
    var equips = actor.equips();
    var slots = actor.equipSlots();
    for (var i = 0; i < slots.length; i++) {
        if (slots[i] === etypeId) {
            list.push(equips[i]);
        }
    }
    var paramId = this.paramId();
    var worstParam = Number.MAX_VALUE;
    var worstItem = null;
    for (var j = 0; j < list.length; j++) {
        if (list[j] && list[j].params[paramId] < worstParam) {
            worstParam = list[j].params[paramId];
            worstItem = list[j];
        }
    }
    return worstItem;
};

Window_ShopStatus.prototype.drawActorData = function() {
    this.drawActorDisplayed();
    this.drawParamName();
    this.drawActorStatInfo();
};

Window_ShopStatus.prototype.drawActorDisplayed = function() {
    for(var actorId = 0; actorId < $gameParty.members().length; ++actorId){
        var actor = $gameParty.members()[actorId];
        var canEquip = actor.canEquip(this._item) && actor.equipSlots().includes(this._item.etypeId);

        this.changeTextColor(this.textColor(0));
        this.setSmallerFont();
        this.contents.fontSize = Yanfly.Param.ItemQuantitySize;

        var rect = this.getRectPositionActor(0, actor.actorId());
        rect.x += this.textPadding();
        rect.width -= this.textPadding() * 2;

        var bitmapName = 'head' + actor.actorId() + ((actor.actorId()==1 && $gameSwitches.value(299)) ? "a" : "");
        this._actorSprite[actorId].x = rect.x + rect.width - 12;
        this._actorSprite[actorId].y = rect.y - 24;
        this._actorSprite[actorId].bitmap = ImageManager.loadBitmap('img/system/actors/', bitmapName);
        if (!canEquip){
            this._actorSprite[actorId].setColorTone([0, 0, 0, 255]);
            this._actorSprite[actorId].opacity = 128;
        }else{
            this._actorSprite[actorId].setColorTone([0, 0, 0, 0]);
            this._actorSprite[actorId].opacity = 255;
        }
    }
};

// 绘制属性名称
Window_ShopStatus.prototype.drawParamName = function() {
    for (var i = 0; i < 8; ++i) {
        var rect1 = this.getRectPosition(i);
        rect1.x += this.textPadding();
        rect1.width = this.textWidth(TextManager.param(1));

        this.changePaintOpacity(true);
        this.changeTextColor(this.textColor(0));
        this.setSmallerFont();
        this.contents.fontSize = Yanfly.Param.ItemQuantitySize;
        if (i<2) {
            var text = TextManager.param(i);
            this.drawText(text, rect1.x, rect1.y, rect1.width, 'right');
        } else if (i==2){
            this.drawText("体力上限", rect1.x, rect1.y, rect1.width, 'right');
        } else{
            if (i<9){
                var text = TextManager.param(i-1);
                this.drawText(text, rect1.x, rect1.y, rect1.width, 'right');

            }
        }
    }
}

// 绘制属性数值
Window_ShopStatus.prototype.drawActorStatInfo = function() {
    for(var actorId = 0; actorId < $gameParty.members().length; ++actorId){
        var actor = $gameParty.members()[actorId];
        var item1 = this.currentEquippedItem(actor, this._item.etypeId);
        for (var i = 0; i < 8; ++i) {
            var rect = this.getRectPositionActor(i,actor.actorId());
            rect.x += this.textPadding();
            rect.width -= this.textPadding() * 2;
            this.changePaintOpacity(true);
            this.changeTextColor(this.textColor(0));
            
            if (i<2) {
                this.drawActorChange(actor, rect, item1, i);
            } else if (i==2){
                this.drawActorTpChange(actor, rect, item1);
            } else{
                this.drawActorChange(actor, rect, item1, i-1);
            }
            this.changePaintOpacity(true);
        }
    }
}

//数值变更
Window_ShopStatus.prototype.drawActorChange = function(actor, rect, item1, i) {
    var canEquip = actor.canEquip(this._item) && actor.equipSlots().includes(this._item.etypeId);
    this.contents.fontSize = Yanfly.Param.ItemQuantitySize;
    if (!canEquip){
        //this.changeTextColor(this.textColor(10));
        //this.drawText('×', rect.x, rect.y, rect.width - 4, 'right');
    }else if(this._item === item1){
        this.changeTextColor(this.textColor(6));
        this.changePaintOpacity(false);
        this.drawText('*', rect.x, rect.y, rect.width - 6, 'right');
    }else{
        var change = this._item.params[i]
        change -= (item1 ? item1.params[i] : 0);
        var text = (change > 0 ? '+' : '') + Yanfly.Util.toGroup(change);
        if (change != 0){
            this.changeTextColor(this.paramchangeTextColor(change));
            this.changePaintOpacity(true);
            this.drawText(text, rect.x, rect.y, rect.width, 'right');
        }else{
            this.changeTextColor(this.textColor(0));
            this.changePaintOpacity(false);
            this.drawText('-', rect.x, rect.y, rect.width - 6, 'right');
        }
    }
};

//Tp变更
Window_ShopStatus.prototype.drawActorTpChange = function(actor, rect, item1) {
    var canEquip = actor.canEquip(this._item) && actor.equipSlots().includes(this._item.etypeId);
    this.contents.fontSize = Yanfly.Param.ItemQuantitySize;
    if (!canEquip){
        //this.changeTextColor(this.textColor(10));
        //this.drawText('×', rect.x, rect.y, rect.width - 4, 'right');
    }else if(this._item === item1){
        this.changeTextColor(this.textColor(6));
        this.changePaintOpacity(false);
        this.drawText('*', rect.x, rect.y, rect.width - 6, 'right');
    }else{
        var change = this._item.tpPlus;
        change -= (item1 ? item1.tpPlus : 0);

        var text = (change > 0 ? '+' : '') + Yanfly.Util.toGroup(change);
        if (change != 0){
            this.changeTextColor(this.paramchangeTextColor(change));
            this.changePaintOpacity(true);
            this.drawText(text, rect.x, rect.y, rect.width, 'right');
        }else{
            this.changeTextColor(this.textColor(0));
            this.changePaintOpacity(false);
            this.drawText('-', rect.x, rect.y, rect.width - 6, 'right');
        }
    }
};

Window_ShopStatus.prototype.changePaintOpacity = function(enabled) {
    this.contents.paintOpacity = enabled ? 255 : this.translucentOpacity();
};

Window_ShopStatus.prototype.paraNameWidth = function(){
    return this.textWidth(TextManager.param(0)) + this.textPadding();
};


Window_ShopStatus.prototype.paraNumWidth = function(){
    return this.textWidth('+0000') + this.textPadding();
};

// 数值对比rect(总体、显示名称用)
Window_ShopStatus.prototype.getRectPosition = function(index) {
    var rect = new Rectangle();
    rect.width = this.paraNameWidth() + 4 * this.paraNumWidth();
    rect.height = this.lineHeight();
    rect.x = 0;
    rect.y = index * this.lineHeight() + this.lineHeight() * 1.2;
    return rect;
};

// 数值对比rect(各角色数值、一列)
Window_ShopStatus.prototype.getRectPositionActor = function(index, actorId) {
    var rect = new Rectangle();
    rect.width = this.paraNumWidth();
    rect.height = this.lineHeight();
    rect.x = this.paraNameWidth() + (actorId-1) * this.paraNumWidth() + 8;
    rect.y = index * this.lineHeight() + this.lineHeight() * 1.2;
    return rect;
};